angular.module('tab-pay', [])
.controller 'TabPayCtrl', (Toast,wechatUtil,$location, $localStorage, $ionicLoading, $scope, $state, $http, $window, $ionicHistory, $ionicPopup, ListDataService, $ionicScrollDelegate, ionicDatePicker, $filter) ->
#返回
    $scope.order = undefined
    loadData = ->
#获取订单详情
    $http.get("/order/#{$state.params.id}",params: {history:0})
    .success (resp) ->
        $scope.order = resp.message
        $scope.order.hasServe = _.countBy $scope.order.orderTracks,(item)->
            item.state == 2
        console.log $scope.order.hasServe
    .error (resp, status) ->
        Toast.showBusinessError(resp.message)
    loadData()

    $scope.back = ->
        $state.go 'tab.order-list'
    paySuccess = ->
        $state.go('tab.orderDetail', {id: $scope.order.id})
    payFail = ->
        Toast.showMsg '支付失败'
    payCancel = ->
        Toast.showMsg '用户取消支付'
    $scope.pay = ->
        console.log "pay"
        $http.get('/payment/wechat/prepay/' + $scope.order.invoice, params: {openId: $localStorage['openId']})
        .success (resp) ->
            if resp.code == 0
                wechatUtil.jsPay(resp.message.prepay_id, resp.message.timeStamp, resp.message.nonceStr, resp.message.sign, paySuccess, payFail, payCancel)
            else
                Toast.showMsg resp.message
        .error (err)->
            console.log(err)
